package com.xidian.zhiancloud.service.chemicalsManagement.healthManger;

import com.xidian.zhiancloud.dao.HealthReportMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.HealthReport;
import com.xidian.zhiancloud.model.HealthReportExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Egoist
 * @Date: 2020/11/10 17:00
 */

@Service
public class HealthReportService {

    @Autowired
    private HealthReportMapper healthReportMapper;

    public void add(HealthReport healthReport) {
        int result = healthReportMapper.insert(healthReport);
        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public HealthReport getByIds(Long id) {
        HealthReport healthReport = healthReportMapper.selectByPrimaryKey(id);
        if (healthReport == null) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        return healthReport;
    }


    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            int result = healthReportMapper.deleteByPrimaryKey(id);
            if (result == 0) {
                throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
            }
        }
    }

    public void patchById(HealthReport healthReport) {
        int result = healthReportMapper.updateByPrimaryKeySelective(healthReport);
        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public List<HealthReport> getList(String key, Long companyId, int current, int size) {
        HealthReportExample healthReportExample = new HealthReportExample();
        HealthReportExample.Criteria criteria = healthReportExample.createCriteria();

        criteria.andCompanyIdEqualTo(companyId);

        if (key != null && !"".equals(key)) {
            criteria.andNameEqualTo('%' + key + '%');
        }
        return healthReportMapper.selectByExampleWithRowbounds(healthReportExample, new RowBounds(current, size));
    }
}
